<?php
include_once("conn.php");
session_start();

function show()
{
	echo '<div class="heading">';
		echo '<div class="cate_link">';
			echo $_POST["newcategory"];
		echo '</div>';
		echo '<div class="controls">';
			echo '<img src="png/tiny_plus.png">';
			echo '<img src="png/tiny_gear.png">';
		echo '</div>';
	echo '</div>';
	
	$sql="insert into category(cate_name,name) values('".$_POST["newcategory"]."','".$_SESSION["current_user"]."')";
	mysql_query($sql);

}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mycss3.css" />
<meta charset="utf-8">
<script type="text/javascript" src="jquery-1.7.1.js"></script>
<script type="text/javascript">
$(function(){
	$("#btn1").click(function(){
		$("#Login").slideToggle("slow");
		$("#Login_go").slideToggle("slow");
	});
	$("#go_back").click(function(){
		$("#Login").slideToggle("slow");
		$("#Login_go").slideToggle("slow");
	});
	$("#login1").click(function(){
		$("#Login").slideToggle("fast");
		$("#Login_go").slideToggle("fast");
	});
	
	var isDoingLogin = false;
	$("#bt_login").click(function(){
		var name1=$("#username").val();
		var pass1=$("#password").val();

		if (isDoingLogin)
		{
			return;
		}
		isDoingLogin = true;

		$.ajax({
			type:"POST",
			url:"login_process.php",
			dataType:"json",
			data:{"name2":name1,"pass2":pass1},
			beforeSend:function(){
				var div1="<p>正在登录中，请稍候...</p>";
				$("#Login_go").append(div1);
			},
			success:function(json){
				$("#b").remove();
				var div2="<li style='float:right;padding:0 3px;'>"+"<a href='manage.php'>"+"欢迎回来"+json.u1+"</a></li>";
				$("#a").append(div2);
			},
			error: function() {
				isDoingLogin = false;
			}

		});
	});
});

</script>
<script>
	function show_add_link(a)
	{
		var obj=document.getElementById(a);
		if(obj.style.display=="none")
		{
			obj.style.display='block';
		}
		else
		{
			obj.style.display='none';
		}
	}

	function modify_cate(a) 
	{
		var obj=document.getElementById(a);

		if(obj.style.display=="none")
		{
			obj.style.display='block';
		}
		else
		{
			obj.style.display='none';
		}

	}
	
	function changeLink(a)
	{
		var obj=document.getElementById(a);

		if(obj.style.display=="none")
		{
			obj.style.display='block';
		}
		else
		{
			obj.style.display='none';
		}

	}
</script>

</head>
<body>

<div id="Header">
	<ul id="a">
		<li><a href="index.php">首页</a></li>
		<li><a href="#">发现</a></li>
		<?php
		if($_SESSION["current_user"] == NULL )
		{
			echo '<li style="float:right;padding:0 3px;"><a href="reg.php">注册</a></li>';
			echo '<li id="b" style="float:right;padding:0 3px;"><a id="login1" href="#">登录</a></li>';
		}
		else
		{
			echo '<li style="float:right;padding:0 3px;"><a href="login_out.php">注销</a></li>';
			echo '<li style="float:right;padding:0 3px;"><a href="manage.php">'.$_SESSION["current_user"].'</a></li>';
		}
		?>
	</ul>

</div>

<div id="Login">
	<form>
		<input type="button" name="btn1" id="btn1" value="登录" />
	</form>
</div>

<div id="Login_go" style="display:none;">
	<form action="login_go.php" method="post" id="login_form">
		<tr>用户名<input type="text" name="username" id="username" /></tr>
		<tr>密码：<input type="password" name="password" id="password"/></tr>
		<tr><input type="button" value="登录" name="login" id="bt_login" /></tr>
		<tr><input type="reset" value="重置"  /></tr>
	</form>
	<tr><input type="button" id="go_back" value="返回" /></tr>
</div>

<div id="Content">
	
	<div id="Create">
		<form method="post" action="index.php">
			<input type="text" class="cate" name="newcategory" id="newcategory" placeholder="input your new category name" />
			<input type="submit" class="submit" value="Create" name="Create" />
		</form>
	</div>
	
	<div id="wrapper">
		<?php
		if($_SESSION["current_user"] != NULL )
		{
			$query1 = "select cate_name,cid from category where name='".$_SESSION["current_user"]."'";
			$result = mysql_query($query1);
			while($gotCate = mysql_fetch_array($result) )
			{
				
				echo '<div class="heading">';
					echo '<div class="cate_link">';
					echo $gotCate["cate_name"];
					echo '</div>';
					echo '<div class="controls">';
					echo "<a href='#' onclick=show_add_link('link_".$gotCate["cid"]."');>";
					echo '<img src="png/tiny_plus.png">';
					echo '</a>';
					echo "<a href='#' onclick=modify_cate('".$gotCate["cid"]."');>";
					echo '<img src="png/tiny_gear.png">';
					echo '</a>';
					echo '</div>';
				echo '</div>';
				
				
				if( $_POST['new_webname'] != NULL && $_POST['new_webaddress'] != null )
				{
					$query = "insert into link(webname,webaddress) values('".$_POST["new_webname"]."','".$_POST["new_webaddress"]."')";
					mysql_query($query);
					$query = "select lid from link where webname='".$_POST["new_webname"]."'";
					$result = mysql_query($query);
					while($lid = mysql_fetch_array($result) )
					{
						$lid1=$lid["lid"];
					}
					$cid = $gotCate["cid"];
					$query="insert into cate_link(cid,lid) values('".$cid."','".$lid1."') ";
					mysql_query($query);

				}
				
				/*下面这个是修改类名*/
				echo "<div id='".$gotCate["cid"]."' class='cateModify' >";
				echo "<input type='text' value='".$gotCate["cate_name"]."' />";
				echo "<input type='button' value='保存' />";
				echo "<input type='button' value='删除类' />";
				echo '</div>';
				/*上面这个是修改类名字*/
				/*下面这个是类里增加链接*/
				echo "<div id='link_".$gotCate["cid"]."' class='linkAdd'>";
				echo "<form action='index.php' method='post'>";
				echo "<input type='text' name='new_webname' id='new_webname' placeholder='new webname' />";echo "&nbsp&nbsp";
				echo "<input type='text' name='new_webaddress' id='new_webaddress' placeholder='new webaddress' />";echo "&nbsp&nbsp";
				echo '<input type="submit" value="添加" />';
				echo "</form>";
				echo "</div>";
				/*上面这个是类里增加链接*/
				$sql2="select category.cate_name,link.lid,webname,webaddress from category,cate_link,link where category.cid=cate_link.cid && link.lid=cate_link.lid ";
				$result2=mysql_query($sql2);
				while($gotLink = mysql_fetch_array($result2) )
				{
					if($gotCate["cate_name"] == $gotLink["cate_name"] )
					{
						echo '<div class="link">';
						echo '<div class="link_name">';
						echo $gotLink["webname"];
						echo $gotLink["webaddress"];
						echo '</div>';

						echo "<div class='controls' onclick=changeLink('".$gotLink["lid"]."'); >";
						echo '<a href="#" onclick=><img src="png/tiny_gear.png"></a>';
						echo '</div>';
						echo '</div>';
						/*以下是修改LINK*/
						echo "<div class='linkMofify' id='".$gotLink["lid"]."' style='display:none;'>";
						echo "<input type='text' value='".$gotLink["webname"]."' />";
						echo "<input type='text' value='".$gotLink["webaddress"]."' />";
						echo "<input type='button' value='保存' />";
						echo "<input type='button' value='删除' />";
						echo "</div>";
						/*以上是修改LINK*/
					}	
				}
			}
			
		}
		?>
	</div>

	<div class="receive_cate">
	<?php
	if($_POST["newcategory"] != NULL && $_SESSION["current_user"]!=NULL )
	{
		show();
		echo "<meta http-equiv='refresh' content='0.5'; url='index.php' />";
	}
	
	?>
	</div>
	

</div>

<div id="Footer">
</div>


</body>
</html>